package com.zzptc.myapp1028;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {
 ListView listView;
 List<Book> list_Book;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        listView=findViewById(R.id.t_book);
        list_Book=new ArrayList<>();

        //1.数据源
        Book book=new Book(1,R.mipmap.lx,"罗翔讲法律",23);
        Book book1=new Book(2,R.mipmap.lx2,"罗翔讲刑法",30);
        Book book2=new Book(3,R.mipmap.lx2,"罗翔与张三",36);
        Book book3=new Book(4,R.mipmap.lx2,"罗翔讲法律2",39);
        Book book4=new Book(5,R.mipmap.lx2,"罗翔讲法律3",50);


        list_Book.add(book);
        list_Book.add(book1);
        list_Book.add(book2);
        list_Book.add(book3);
        list_Book.add(book4);

        MyAdapter myAdapter=new MyAdapter();
        listView.setAdapter(myAdapter);//讲ListView对象关联


    }

    //2.绑定数据
    class MyAdapter extends BaseAdapter{

        @Override
        public int getCount() {
            return list_Book.size();
        }

        @Override
        public Object getItem(int position) {
            return null;
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            //读出一条数据
            Book book=list_Book.get(position);
            //绑定(先做一个小界面)
            View view=View.inflate(MainActivity.this,R.layout.item_book,null);

            ImageView b_pic= view.findViewById(R.id.b_pic);//找小界面上的控件
            b_pic.setImageResource(R.mipmap.lx2);//数据绑定

            TextView b_id=  view.findViewById(R.id.b_id);//找小界面上的控件
            b_id.setText(book.getId()+"");//数据绑定

            TextView b_name= view.findViewById(R.id.b_name);
            b_name.setText(book.getName());

            TextView b_price= view.findViewById(R.id.b_price);
            b_price.setText(book.getPrice()+"");







            return  view;
        }
    }
}
